package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductType;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.IProductTypeService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductTypeService typeService;
    @Autowired
    private ServletContext ctx;
    /**
     * 获取商品页面的精品推荐商品和普通商品
     */
    @GetMapping(produces = "application/json;chartset=utf-8")
    @ResponseBody
    public Object recommend(ProductQueryObject qo){
        //精品推荐
            PageInfo query = productService.query(qo);
            return query;
    }
    @GetMapping(produces = "text/html;chartset=utf-8")
    public void productList(ProductQueryObject qo,HttpServletResponse responsep) throws Exception {
        //普通商品
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
            cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
            Template temp = cfg.getTemplate("product.ftl", "UTF-8");
            PageInfo query = productService.query(qo);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("query",query);
            temp.process(map,responsep.getWriter());
    }
    /**
     * 导航栏的分类数据
     * @param
     * @return
     */
    @GetMapping("/productType")
    @ResponseBody
    public Object getProductType(){
        List<ProductType> productTypes = typeService.listAll();
        return productTypes;
    }
    /**
     * 商品明细
     */
    @GetMapping("/{id}")
    @ResponseBody
    public Object getProduct(@PathVariable Long id){
        System.out.println("ssss");
        Product product = productService.get(id);
        return product;
    }
    @PostMapping
    @ResponseBody
    public Object addShopCar(Long id, HttpServletRequest request){
        JSONResult jsonResult = new JSONResult();
        try {
            //如果是第一次加入购物车则创建一个集合
            List<Long> productids =(List) (request.getSession().getAttribute("SEESION_IN_PRODUCTS"));
            if (productids == null) {
                productids = new ArrayList<Long>();
                productids.add(id);
                request.getSession().setAttribute("SEESION_IN_PRODUCTS",productids);
            }else{
             //如果不是就往session中加数据
                if(productids.contains(id)){
                    throw new Exception("亲,已经在购物车中,去结账");
                }
                request.getSession().getAttribute("SEESION_IN_PRODUCTS");
                productids.add(id);
                request.getSession().setAttribute("SEESION_IN_PRODUCTS",productids);
            }
            jsonResult.setData(productids);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    //从购物车中删除
    @DeleteMapping("/cart")
    @ResponseBody
    public Object deleteShopCar(Long id, HttpServletRequest request){
        JSONResult jsonResult = new JSONResult();
        try {
            //如果是第一次加入购物车则创建一个集合
            List<Long> productids =(List) (request.getSession().getAttribute("SEESION_IN_PRODUCTS"));
            //如果不是就往session中加数据
            if(productids!=null && productids.size()>0){
                if(productids.contains(id)){
                    productids.remove(id);
                }
                request.getSession().setAttribute("SEESION_IN_PRODUCTS",productids);
            }else{
                throw new Exception("没事不要瞎点");
            }
            jsonResult.setData(productids);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    /**
     * 购物车
     * @param
     * @return
     */
    @GetMapping(value = "/cart")
    @ResponseBody
    public Object shoppingCart(HttpServletRequest request){
        List<Long> productids =(List<Long>) (request.getSession().getAttribute("SEESION_IN_PRODUCTS"));
        List<Product> list=productService.getShoppingCart(productids);
        return list;
    }
}
